﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Interop;

namespace 获取屏幕区域截图
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //this.image.Source = DisplayBitmapImage(bmap);
        }

        // 用于释放 HBITMAP 资源
        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        public static extern bool DeleteObject(IntPtr hObject);

        /// <summary>
        /// 将 Bitmap 转换为 BitmapImage 并显示
        /// </summary>
        /// <param name="bitmap"></param>
        private void DisplayBitmapImage(Bitmap bitmap)
        {
            // 将 System.Drawing.Bitmap 转换为 BitmapSource
            IntPtr hBitmap = bitmap.GetHbitmap(); // 获取 HBITMAP 指针

            // 使用 WPF 的 Imaging.CreateBitmapSourceFromHBitmap 方法将 HBITMAP 转换为 BitmapSource
            BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(
                hBitmap,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());

            // 将 BitmapSource 设置为 Image 控件的 Source
            image.Source = bitmapSource;

            // 释放 HBITMAP 资源
            DeleteObject(hBitmap);
        }

        /// <summary>
        /// 根据位置进行截图
        /// </summary>
        /// <param name="x">left</param>
        /// <param name="y">top</param>
        /// <param name="width">width</param>
        /// <param name="height">height</param>
        /// <returns></returns>
        public static Bitmap CaptureScreen(int x, int y, int width, int height)
        {
            Bitmap bmp = new Bitmap(width, height);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.CopyFromScreen(x, y, 0, 0, new System.Drawing.Size(width, height));
            }
            return bmp;
        }

        /// <summary>
        /// 保存截图到本地
        /// </summary>
        /// <param name="screenshot">bitmap data</param>
        private void SaveScreenshot(Bitmap screenshot)
        {  
            var saveFileDialog = new Microsoft.Win32.SaveFileDialog
            {
                Filter = "PNG Files (*.png)|*.png"
            };

            if (saveFileDialog.ShowDialog() == true)
            {
                screenshot.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Png);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var bmap = CaptureScreen(0, 0, 1500, 800);
            DisplayBitmapImage(bmap);


        }
    }
}
